/*
 * Copyright 2012 NJUT  qixiaobo. All rights reserved.
 */
package com.fujitsu.nanjing.tieba.controller.section;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.fujitsu.nanjing.tieba.base.BaseController;
import com.fujitsu.nanjing.tieba.domain.BoardInfo;
import com.fujitsu.nanjing.tieba.exception.Resource404Exception;
import com.fujitsu.nanjing.tieba.service.BoardInfoService;

/**
 * BoardController.java
 * 
 * @author Qixiaobo-win8
 */
@Controller
@RequestMapping("/forum/boards")
public class BoardController extends BaseController {
	@Autowired
	private BoardInfoService boardInfoService;
	@RequestMapping(value = "/{boardid}", method = RequestMethod.GET)
	public String showBoard(Model model, @PathVariable String boardid)
			throws Resource404Exception {
		BoardInfo boardInfo = boardInfoService.findById(boardid);
		if (boardInfo == null) {
			throw new Resource404Exception("404,你懂的！");
		}
		model.addAttribute("boardInfo", boardInfo);
		return "board/board";
	}

	@RequestMapping(value = "/{boardid}/pages/{pageNum}", method = RequestMethod.GET)
	public String showBoardWithPageNum(Model model,
			@PathVariable String boardid, @PathVariable String pageNum)
			throws Resource404Exception {
		BoardInfo boardInfo = boardInfoService.findByIdWithPageNum(boardid,
				Integer.parseInt(pageNum));
		if (boardInfo == null) {
			throw new Resource404Exception("404,你懂的！");
		}
		pageNum = (pageNum == null ? "0" : pageNum);
		model.addAttribute("pageIndex", pageNum);
		model.addAttribute("boardInfo", boardInfo);
		return "board/board";
	}
}
